#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UISectionTable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import "iMarks.h"
#import "SubjectView.h"
#import "SubjectsManager.h"
#import "SubjectDetailView.h"

@implementation iMarks

- (void) applicationDidFinishLaunching: (id) unused
{
    UIWindow *window;
    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
   
	rect.origin.x = rect.origin.y = 0.0f;

    window = [[UIWindow alloc] initWithContentRect: [UIHardware
        fullScreenApplicationContentRect]];
	
	buttonBar = [[UIButtonBar alloc]
				 initInView:mainView
				 withFrame:CGRectMake(0.0f,  rect.size.height-49.0f,
									  rect.size.width, 49.0f)
				 withItemList: [self buttonBarItems]];
	
    [buttonBar setDelegate:self];
    [buttonBar setBarStyle:1];
    [buttonBar setButtonBarTrackingMode: 2];

    int buttons[3] = { 1, 2, 3};
    [buttonBar registerButtonGroup:0 withButtons:buttons withCount: 3];
    [buttonBar showButtonGroup: 0 withDuration: 0.0f];

	[[buttonBar viewWithTag:1 ] setFrame:CGRectMake(2.0f, 1.0f, 64.0f, 48.0f)];
	[[buttonBar viewWithTag:2 ] setFrame:CGRectMake(66.0f, 1.0f, 64.0f, 48.0f)];
	[[buttonBar viewWithTag:3 ] setFrame:CGRectMake(2.0f + 256.0f - 10.0f, 1.0f,
													70.0f, 48.0f)];

    [ buttonBar showSelectionForButton: 1];

    [window orderFront: self];
    [window makeKey: self];
    [window _setHidden: NO];
	[SubjectsManager sharedInstance];

	
	mainView = [[UIView alloc] initWithFrame: rect];
	
	contentView = [[UITransitionView alloc] 
				   initWithFrame: CGRectMake(0.0f, 0.0f,
											 rect.size.width,
											 rect.size.height - 49.0f)];
	
	marksView = [[MarksView alloc]
				 initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, 
										   rect.size.height - 49.0f)];
	
	settingsView = [[SettingsView alloc] 
					initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, 
											  rect.size.height - 49.0f)
					andMainView:mainView];
	
	timeTableView = [[TimeTableView alloc] 
					 initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, 
											   rect.size.height - 49.0f)];
	
	[contentView addSubview:marksView];
	
    [mainView addSubview: contentView];
	[mainView addSubview: buttonBar];
	
    [window setContentView: mainView];
		
	defaultPNG = [self createApplicationDefaultPNG];
}

- (NSArray *)buttonBarItems {
    return [ NSArray arrayWithObjects:
        [ NSDictionary dictionaryWithObjectsAndKeys:
          @"buttonBarItemTapped:", kUIButtonBarButtonAction,
          @"imageup.png", kUIButtonBarButtonInfo,
          @"imagedown.png", kUIButtonBarButtonSelectedInfo,
          [ NSNumber numberWithInt: 1], kUIButtonBarButtonTag,
            self, kUIButtonBarButtonTarget,
          @"Noten", kUIButtonBarButtonTitle,
          @"0", kUIButtonBarButtonType,
          nil 
        ],
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"imageup.png", kUIButtonBarButtonInfo,
			 @"imagedown.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 2], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"Stundenplan", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil 
			 ],
        [ NSDictionary dictionaryWithObjectsAndKeys:
          @"buttonBarItemTapped:", kUIButtonBarButtonAction,
          @"imageup.png", kUIButtonBarButtonInfo,
          @"imagedown.png", kUIButtonBarButtonSelectedInfo,
          [ NSNumber numberWithInt: 3], kUIButtonBarButtonTag,
            self, kUIButtonBarButtonTarget,
          @"Einstellungen", kUIButtonBarButtonTitle,
          @"0", kUIButtonBarButtonType,
          nil 
        ], 
        nil
    ];
}

- (void)buttonBarItemTapped:(id) sender {
    int button = [ sender tag ];
	switch (button) {
		case 1:
			[self setStatusBarMode:0 duration:0];
			[contentView transition:0 toView:marksView];
			break;
		case 2:
			[self setStatusBarMode:1 duration:0];
			[contentView transition:0 toView:timeTableView];
			break;
		default:
			[self setStatusBarMode:0 duration:0];
			[contentView transition:0 toView:settingsView];
			break;
	}
	[settingsView save];
}

- (void)applicationWillTerminate {
	if (defaultPNG != nil) {
		NSString *pathToDefault = [NSString stringWithFormat:@"%@/Default.png", 
								   [[NSBundle mainBundle] bundlePath]];
		NSURL *urlToDefault = [NSURL fileURLWithPath:pathToDefault];
		CGImageDestinationRef dest = CGImageDestinationCreateWithURL(
														(CFURLRef)urlToDefault, 
														CFSTR("public.png"), 1,
														NULL);
		CGImageDestinationAddImage(dest, defaultPNG, NULL);
		CGImageDestinationFinalize(dest);
		CFRelease(defaultPNG);
	}
	[[SubjectsManager sharedInstance] writeFile];
}

@end